package model.person.post;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import model.person.Person;

@Entity
@Table(name = "post")
public class Post implements Serializable {

	private static final long serialVersionUID = -8740338517284408781L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	Long id;

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "source_id")
	Person sourcePerson;

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "id_destination")
	Person destinationPerson;

	String postContent;

	Date createdDate;

	public Post() {
	}

	public Post(Person sourcePerson, Person destinationPerson, String postContent) {
		super();
		this.sourcePerson = sourcePerson;
		this.destinationPerson = destinationPerson;
		this.postContent = postContent;
		setCreatedDate();
	}

	public Person getSourcePerson() {
		return sourcePerson;
	}

	public void setSourcePerson(Person sourcePerson) {
		this.sourcePerson = sourcePerson;
	}

	public Person getDestinationPerson() {
		return destinationPerson;
	}

	public void setDestinationPerson(Person destinationPerson) {
		this.destinationPerson = destinationPerson;
	}

	public String getPostContent() {
		return postContent;
	}

	public void setPostContent(String postContent) {
		this.postContent = postContent;
	}

	public Long getId() {
		return id;
	}

	public Date getCreatedDate() {
		return createdDate;
	}

	public String getCreatedDateString() {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd | HH:mm:ss");
		return format.format(createdDate);
	}

	public void setCreatedDate() {
		this.createdDate = Calendar.getInstance().getTime();// Calendar.getInstance().getTime();
	}

	@Override
	public String toString() {
		return "Post [id=" + id + ", sourcePerson=" + sourcePerson + ", destinationPerson=" + destinationPerson + ", postContent=" + postContent + ", createdDate=" + createdDate + "]";
	}
}
